package com.kyzh.core.pager.weal.community.adapter

import android.app.Activity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.beans.CommunityPingLunBean
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemCommunityPinglunBinding
import com.kyzh.core.databinding.ItemCommunityPinglunchildBinding
import com.kyzh.core.dialog.showSendMessageDialog
import com.kyzh.core.pager.weal.community.viewmodel.CommunityViewModel
import com.kyzh.core.utils.setVisibility

class PingLunAdapter(val vm: CommunityViewModel) : BaseQuickAdapter<CommunityPingLunBean.Data,BaseDataBindingHolder<ItemCommunityPinglunBinding>>(
    R.layout.item_community_pinglun),LoadMoreModule {
    init {
        addChildClickViewIds(R.id.tvZan,R.id.back)
    }
    override fun convert(
        holder: BaseDataBindingHolder<ItemCommunityPinglunBinding>,
        item: CommunityPingLunBean.Data
    ) {
        holder.dataBinding?.apply {
            d = item
            title.setVisibility(holder.layoutPosition==0 )
            val pingLunChildAdapter = PingLunChildAdapter(item.list)
            rev.adapter =pingLunChildAdapter
            pingLunChildAdapter.setOnItemChildClickListener{a,v,p->
                val data = a.data[p] as CommunityPingLunBean.Data
                when(v.id){
                    R.id.childZan ->{
                        vm.zan(1,data.id,data.uid){
                            if(this ==1){
                                kotlin.runCatching {
                                    if(data.isGood){
                                        data.good = (data.good.toInt()-1).toString()
                                    }else{
                                        data.good = (data.good.toInt()+1).toString()
                                    }
                                }
                                data.isGood =!data.isGood
                                a.notifyItemChanged(p)
                            }
                        }
                    }
                    R.id.back ->{
                        (context as Activity).showSendMessageDialog("回复@${data.petName}") {
                            vm.huifu("1",this,item.id,data.uid){

                            }
                        }
                    }
                }
            }
        }
    }
}

class PingLunChildAdapter( array: ArrayList<CommunityPingLunBean.Data>): BaseQuickAdapter<CommunityPingLunBean.Data,BaseDataBindingHolder<ItemCommunityPinglunchildBinding>>(R.layout.item_community_pinglunchild,array) {
    init {
        addChildClickViewIds(R.id.back,R.id.childZan)
    }
    override fun convert(
        holder: BaseDataBindingHolder<ItemCommunityPinglunchildBinding>,
        item: CommunityPingLunBean.Data
    ) {
       holder.dataBinding?.apply {
           d = item
       }
    }
}
